package com.jerry.cui.dingtalk.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiUserGetRequest;
import com.dingtalk.api.request.OapiV2UserGetbymobileRequest;
import com.dingtalk.api.request.OapiV2UserUpdateRequest;
import com.dingtalk.api.response.OapiUserGetResponse;
import com.dingtalk.api.response.OapiV2UserGetbymobileResponse;
import com.dingtalk.api.response.OapiV2UserUpdateResponse;
import com.jerry.cui.dingtalk.oauth2.DingtalkOAuth2AccessToken;
import com.jerry.cui.dingtalk.properties.DingtalkProperties;
import com.taobao.api.ApiException;

import lombok.extern.slf4j.Slf4j;

/**
 * 封装钉钉用户操作
 */
@Component
@Slf4j
public class DingTalkUserService {
    @Autowired
    private DingtalkOAuth2AccessToken dingtalkOAuth2AccessToken;
    @Autowired
    private DingtalkProperties dingtalkProperties;

    public OapiUserGetResponse getUser(String userId) throws ApiException {
        DingTalkClient client = new DefaultDingTalkClient(dingtalkProperties.getApi().getUser_get());
        OapiUserGetRequest request = new OapiUserGetRequest();
        request.setUserid(userId);
        request.setHttpMethod("GET");
        return client.execute(request, dingtalkOAuth2AccessToken.getAccessToken());
    }
    
    public OapiV2UserGetbymobileResponse getbymobile(String mobile) throws ApiException {
    	DingTalkClient client = new DefaultDingTalkClient(dingtalkProperties.getApi().getUser_getbymobile());
    	OapiV2UserGetbymobileRequest request = new OapiV2UserGetbymobileRequest();
    	request.setMobile(mobile);
    	return client.execute(request, dingtalkOAuth2AccessToken.getAccessToken());
    }

    public OapiV2UserUpdateResponse updateUser(OapiV2UserUpdateRequest request) throws ApiException{
        DingTalkClient client = new DefaultDingTalkClient(dingtalkProperties.getApi().getUser_update());
        return client.execute(request, dingtalkOAuth2AccessToken.getAccessToken());
    }


}
